package sort;

import java.util.Arrays;
import java.util.Random;

/**
 * Created With IntelliJ IDEA.
 * Descriptions:
 * User:Mr.Du
 * Date:2022/3/8
 * Time:16:48
 */
public class 选择排序 {

    public static void main(String[] args) {
        int[] arr = new int[10];
        Random r = new Random();
        for(int j = 0;j<10;j++){
            for(int i = 0;i<arr.length;i++){
                arr[i] = r.nextInt(50);
            }
            System.out.println("原数组: ");
            System.out.println(Arrays.toString(arr));
            selectSort(arr);
            System.out.println("选择排序后的数组: ");
            System.out.println(Arrays.toString(arr));
        }

    }

    private static void selectSort(int[] arr) {
        for(int i = 0;i<arr.length - 1;i++){
            int h = i;//标记当前最小值
            for(int j = i + 1;j<arr.length;j++){
                if(arr[h] > arr[j]){
                    h = j;
                }
            }
            if(h != i){
                int tmp = arr[i];
                arr[i] = arr[h];
                arr[h] = tmp;
            }
        }
    }
}
